#!/bin/bash
# $1 indicate the path

HTML="index.html"

function Tool(){
    if [ ! -f trim ];then
        echo '
            #include<iostream>
            #include<regex>
            #include<fstream>
            /**
            * @brief get the parameter as the file name
            * keep the file in ext:temp
            */
            void dotHtml(std::string const&inPath,std::string const&outPath){
                std::regex reg("(.*href=\")(.*/)(\".+)",std::regex::icase);
                std::smatch matcher;
                const char* append="index.html";

                std::ifstream ifs(inPath);
                std::ofstream ofs(outPath,std::ios::app);
                char buffer[256];
                std::string temp;
                temp.resize(256);
                if(!ofs){
                    throw std::runtime_error("output file not open");
                }
                if(ifs.is_open()){
                    while(!ifs.eof()){
                        ifs.getline(buffer,256);
                        temp=buffer;
                        std::regex_search(temp,matcher,reg);
                        if(matcher.size()==4)
                            ofs<<matcher.str(1)+matcher.str(2)+append
                                +matcher.str(3)<<std::endl;
                        else ofs<<temp<<std::endl;
                    }
                }


            }
            int main(int argc,char**argv){
                // get tag
                // href="path/"
                int i=0;
                while(++i<argc){
                    dotHtml(argv[i],argv[i]+std::string(".temp"));
                }
                return 0;
            }
        '>index.cpp
        if g++ -std=c++11 index.cpp -otrim
        then
            return 0
        else
            return 1
        fi
    fi
}

function ind(){
    if [ -d $1 ];then
        for file in $(ls $1)
        do
            ind $file
        done
    elif [[ $1 = $HTML ]];then
        ./trim $1 &&
        mv $1.temp $1
    fi
}

Tool
for f in $@
do
    ind $f
done